﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_FileUpload : System.Web.UI.Page
{
    public string CourseID
    {
        get
        {
            if (Request.QueryString["CourseID"] != null)
                return Request.QueryString["CourseID"];
            return "";
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void UploadFile(object sender, EventArgs e)
    {
        System.IO.DirectoryInfo di = null;
        if(CourseID == "")
            di = new System.IO.DirectoryInfo(Server.MapPath("~/TempFiles/" + hidGuid.Value + "/" + hidFolder.Value));
        else
            di = new System.IO.DirectoryInfo(Server.MapPath("~/Course/Courses/" + CourseID + "/" + hidFolder.Value));

        if (!di.Exists)
            di.Create();
        if (filUpload.HasFile)
        {
            if(CourseID == "")
                filUpload.SaveAs(Server.MapPath("~/TempFiles/" + hidGuid.Value + "/" + hidFolder.Value) + @"\" + filUpload.FileName);
            else
                filUpload.SaveAs(Server.MapPath("~/Course/Courses/" + CourseID + "/" + hidFolder.Value) + @"\" + filUpload.FileName);
        }
    }

    protected void MakeXMLFile(object sender, EventArgs e)
    {
        System.IO.DirectoryInfo di;
        if(CourseID == "")
            di = new System.IO.DirectoryInfo(Server.MapPath("~/TempFiles/" + hidGuid.Value + "/" + hidFolder.Value));
        else
            di = new System.IO.DirectoryInfo(Server.MapPath("~/Course/Courses/" + CourseID + "/" + hidFolder.Value));
        if (!di.Exists)
            di.Create();

        string XML = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n" + hidXML.Value;
        System.IO.FileStream fs;
        if(CourseID == "")
            fs = new System.IO.FileStream(Server.MapPath("~/TempFiles/" + hidGuid.Value + "/" + hidFolder.Value + "/") + hidFileName.Value, System.IO.FileMode.Create);
        else
            fs = new System.IO.FileStream(Server.MapPath("~/Course/Courses/" + CourseID + "/" + hidFolder.Value + "/") + hidFileName.Value, System.IO.FileMode.Create);
        System.IO.StreamWriter sw = new System.IO.StreamWriter(fs);
        sw.Write(XML);
        sw.Close();
        fs.Close();
    }
}
